package sample.util;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sample.model.ExtremeData;
import sample.model.StandardData;

import java.util.ArrayList;
import java.util.List;

public class DownloadExcel {

    public static XSSFWorkbook downloadStandardExcel(String name, List<StandardData> standardDatas) {
        List<String> cellName = cellNameList("standard");
        XSSFWorkbook excel = Excel.createExcel(name, cellName);
        Excel.createExcelData(excel, standardDatas);
        return excel;
    }
    public static XSSFWorkbook downloadExtremeExcel(String name, List<ExtremeData> extremeDataList){
        List<String> cellName = cellNameList("extreme");
        XSSFWorkbook excel = Excel.createExcel(name, cellName);
        Excel.createExtremeExcel(excel,extremeDataList);
        return excel;
    }


    public static List<String> cellNameList(String name) {
        ArrayList<String> list = new ArrayList<>();
        if (name.equals("standard")) {
            // 年	月	日	时刻	大气压	干球温度	相对湿度	露点温度	风向	风速	云量	太阳总辐射	法向直射辐射	水平面散射辐射
            list.add("年");
            list.add("月");
            list.add("日");
            list.add("时刻");
            list.add("大气压");
            list.add("干球温度");
            list.add("相对湿度");
            list.add("露点温度");
            list.add("风向");
            list.add("风速");
            list.add("云量");
            list.add("太阳总辐射");
            list.add("法向直射辐射");
            list.add("水平面散射辐射");
        } else if(name.equals("extreme")){
            //台站号	纬度	经度	海拔	类型	小时	干球温度	相对湿度	含湿量	水平面总辐射强度	水平面散射辐射强度
            // 湿球温度	露点温度	水汽压	焓值	法向直射辐射强度	东向总辐射强度	南向总辐射强度	西向总辐射强度
            // 北向总辐射强度	天空有效温度	地表温度	风速	风向	大气压
            list.add("台站号");
            list.add("纬度");
            list.add("经度");
            list.add("海拔");
            list.add("类型");
            list.add("小时");
            list.add("干球温度");
            list.add("相对湿度");
            list.add("含湿量");
            list.add("水平面总辐射强度");
            list.add("水平面散射辐射强度");
            list.add("湿球温度");
            list.add("露点温度");
            list.add("水汽压");
            list.add("焓值");
            list.add("法向直射辐射强度");
            list.add("东向总辐射强度");
            list.add("南向总辐射强度");
            list.add("西向总辐射强度");
            list.add("北向总辐射强度");
            list.add("天空有效温度");
            list.add("地表温度");
            list.add("风速");
            list.add("风向");
            list.add("大气压");
        }
        return list;
    }

}

class Excel {
    /**
     * create Excel
     * @param name sheet 名字
     * @param cellName 列名
     * @return
     */
    public static XSSFWorkbook createExcel(String name, List<String> cellName) {
        XSSFWorkbook excel = new XSSFWorkbook();
        XSSFSheet sheet = excel.createSheet(name);
        int size = cellName.size();
        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < size; i++) {
            row.createCell(i).setCellValue(cellName.get(i));
        }
        return excel;
    }


    /**
     *
     * @param excel
     * @param list 标准气象年数据列表
     * @return
     */
    public static XSSFWorkbook createExcelData(XSSFWorkbook excel, List<StandardData> list) {
        //得到sheet
        XSSFSheet sheet = excel.getSheetAt(0);
        int size = list.size();
        //添加数据到row
        for (int i = 0; i < size; i++) {
            StandardData standardData = list.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            addDataToExcel(row, standardData);
        }
        return excel;
    }
    public static XSSFWorkbook createExtremeExcel(XSSFWorkbook excel,List<ExtremeData> list){
        XSSFSheet sheetAt = excel.getSheetAt(0);
        for (int i = 0; i < list.size(); i++) {
            ExtremeData extremeData = list.get(i);
            XSSFRow row = sheetAt.createRow(i + 1);
            addDataToExtremeExcel(row, extremeData);
        }
        return excel;
    }

    private static void addDataToExtremeExcel(XSSFRow row, ExtremeData extremeData) {
        /**
         *  private int station_no;
         *     private double longtude;
         *     private double latitude;
         *     private double HB;
         *     private int type_id;
         *     private int hour;
         *     private double DBT;
         *     private double RH;
         *     private double HSL;
         *     private double TH;
         *     private double DF;
         *     private double WBT;
         *     private double DPT;
         *     private double VP;
         *     private double EP;
         *     private double NR;
         *     private double EH;
         *     private double SH;
         *     private double WH;
         *     private double NH;
         *     private double VT;
         *     private double ET;
         *     private double WS;
         *     private double WD;
         *     private double AP;
         *
         */
        row.createCell(0).setCellValue(extremeData.getStation_no());
        row.createCell(1).setCellValue(extremeData.getLongtude());
        row.createCell(2).setCellValue(extremeData.getLatitude());
        row.createCell(3).setCellValue(extremeData.getHB());
        row.createCell(4).setCellValue(extremeData.getType_id());
        row.createCell(5).setCellValue(extremeData.getHour());
        row.createCell(6).setCellValue(extremeData.getDBT());
        row.createCell(7).setCellValue(extremeData.getRH());
        row.createCell(8).setCellValue(extremeData.getHSL());
        row.createCell(9).setCellValue(extremeData.getTH());
        row.createCell(10).setCellValue(extremeData.getDF());
        row.createCell(11).setCellValue(extremeData.getWBT());
        row.createCell(12).setCellValue(extremeData.getDPT());
        row.createCell(13).setCellValue(extremeData.getVR());
        row.createCell(14).setCellValue(extremeData.getEP());
        row.createCell(15).setCellValue(extremeData.getNR());
        row.createCell(16).setCellValue(extremeData.getEH());
        row.createCell(17).setCellValue(extremeData.getSH());
        row.createCell(18).setCellValue(extremeData.getWH());
        row.createCell(19).setCellValue(extremeData.getNH());
        row.createCell(20).setCellValue(extremeData.getVT());
        row.createCell(21).setCellValue(extremeData.getET());
        row.createCell(22).setCellValue(extremeData.getWS());
        row.createCell(23).setCellValue(extremeData.getWD());
        row.createCell(24).setCellValue(extremeData.getAP());

    }

    /**
     *
     * @param row excel row
     * @param standardData 每行需要填入的标准数据
     */
    private static void addDataToExcel(XSSFRow row, StandardData standardData) {
        /**
         * private int year;
         *     private int month;
         *     private int day;
         *     private int hour;
         *     private double AP;
         *     private double DBT;
         *     private double RH;
         *     private double DPT;
         *     private double WD;
         *     private double WS;
         *     private int CC;
         *     private double TH;
         *     private double NR;
         *     private double DF;
         */

        row.createCell(0).setCellValue(standardData.getYear());
        row.createCell(1).setCellValue(standardData.getMonth());
        row.createCell(2).setCellValue(standardData.getDay());
        row.createCell(3).setCellValue(standardData.getHour());
        row.createCell(4).setCellValue(standardData.getAP());
        row.createCell(5).setCellValue(standardData.getDBT());
        row.createCell(6).setCellValue(standardData.getRH());
        row.createCell(7).setCellValue(standardData.getDPT());
        row.createCell(8).setCellValue(standardData.getWD());
        row.createCell(9).setCellValue(standardData.getWS());
        row.createCell(10).setCellValue(standardData.getCC());
        row.createCell(11).setCellValue(standardData.getTH());
        row.createCell(12).setCellValue(standardData.getNR());
        row.createCell(13).setCellValue(standardData.getDF());
    }
}
